

package com.l2qq.admin.role;

import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jfinal.aop.Before;
import com.jfinal.aop.Inject;
import com.jfinal.core.Controller;
import com.jfinal.core.Path;
import com.jfinal.kit.Ret;
import com.jfinal.plugin.activerecord.Page;
import com.l2qq.admin.login.LoginService;
import com.l2qq.common.model.AccountRole;
import com.l2qq.common.model.Role;

/**
 * 角色管理控制器
 */
@Path("/admin/role")
public class RoleAdminController extends Controller {
    @Inject
    LoginService loginService;
    @Inject
    RoleAdminService srv;
    @Inject
    AccountRole accountRole;

/*
	@Inject
    PermissionAdminService permissionAdminSrv;
*/

    public void getRoleListPageVue() {
        Page<Role> page = srv.paginate(getInt("currentPage", 1), getInt("pageSize", 10));
        set("page", page);
        renderJson();
    }


    @Before(RoleAdminValidatorVue.class)
    public void saveVue() {
        Role role = getBean(Role.class, "");
        Ret ret = srv.saveVue(role);
        Integer[] roleSubject = getParaValuesToInt("role_subject[]");
        //System.out.println("roleSubject:"  +  roleSubject);
        renderJson(ret);
    }

    public void editVue() {
        keepPara("pn");    // 保持住分页的页号，便于在 ajax 提交后跳转到当前数据所在的页
        Role role = srv.findById(getInt("id"));
        set("role", role);
        renderJson();
    }

    /**
     * 提交修改
     */

    @Before(RoleAdminValidatorVue.class)
    public void updateVue() {
        Role role = getBean(Role.class, "");
        String[] roleSubject = getParaValues("role_subject[]");
        Ret ret = srv.update(role, roleSubject);
        //System.out.println("roleSubject:"  +  roleSubject);
        renderJson(ret);
    }


    public void deleteVue() {
        Ret ret = srv.delete(getInt("id"));
        renderJson(ret);
    }



/*    public void assignPermissionsVue() {
        Role role = srv.findById(getInt("id"));
        List<Permission> permissionList = permissionAdminSrv.getAllPermissions(getInt("id"));
        //srv.markAssignedPermissions(role, permissionList);
        LinkedHashMap<String, List<Permission>> permissionMap = srv.groupByController(permissionList);
        set("role", role);
        set("permissionMap", permissionMap);
        renderJson();
    }*/


    public void assignPermissionSwitchVue() {
        if (getBoolean("checked")) {
            renderJson(srv.addPermission(getInt("role_id"), getInt("permission_id")));
        } else {
            renderJson(srv.deletePermission(getInt("role_id"), getInt("permission_id")));
        }
    }


    public void assignPermissionSwitchListVue() {
        JSONObject jsonObject = JSONObject.parseObject(getRawData(), Feature.OrderedField);
        //System.out.println("jsonObject: " + jsonObject);
        int roleId = jsonObject.getInteger("role_id");
        if (roleId == 1) {
            renderJson(Ret.fail("msg", "超级管理员天然拥有所有权限，无需分配"));
            return;
        }
        boolean checked = jsonObject.getBoolean("checked");
        JSONArray jsonArray = jsonObject.getJSONArray("assignedList");
        for (int i = 0; i < jsonArray.size(); i++) {
            int permissionId = jsonArray.getInteger(i);
            if (checked) {
                srv.addPermission(roleId, permissionId);
            } else {
                srv.deletePermission(roleId, permissionId);
            }
        }
        renderJson(Ret.ok("msg", "批量设置权限成功"));
    }
}



